package com.dmw.month_lx.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.dmw.month_lx.entity.Organization;
import com.dmw.month_lx.entity.PayBill;
import com.dmw.month_lx.enums.TypeEnum;
import com.dmw.month_lx.service.PayBillService;

@Controller
public class PayBillController {

	@Autowired
	private PayBillService payBillService;
	
	
	@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize, PayBill bill,Model model) {
		PageInfo<PayBill> pageInfo = payBillService.selectPayBill(pageNum, pageSize, bill);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("bill", bill);
		return "payBill_list";
	}
	
	@RequestMapping("/toAdd")
	public String insertPayBill(Model model) {
		PayBill bill = new PayBill();
		model.addAttribute("bill", bill);
		return "payBill_insert";
	}
	
	@RequestMapping("/add")
	public String add(@Validated @ModelAttribute("bill")PayBill bill,BindingResult result,MultipartFile file,Model model) throws IllegalStateException, IOException {
		if(result.hasErrors()) {
			return "payBill_insert";
		}
		if(!file.isEmpty()) {
			String originalFilename = file.getOriginalFilename();
			String fileName = System.currentTimeMillis()+"_"+originalFilename;
			File destFile = new File("d:\\imgpic\\"+fileName);
			//复制文件
			file.transferTo(destFile);
			bill.setBills("/imgpic/"+fileName);
		}
		payBillService.insertPayBill(bill);
		return "redirect:list";
	}
	
	@RequestMapping("/getOrgan")
	@ResponseBody
	public List<Organization> getOrgan(Organization organ){
		return payBillService.selectOrganization(organ);
	}
	
	@RequestMapping("/getTypes")
	@ResponseBody
	public List<String> getTypes(){
		List<String> types = new ArrayList<>();
		TypeEnum[] type = TypeEnum.values();
		for (TypeEnum t : type) {
			types.add(t.getLabel());
		}
		return types;
	}
	
	@RequestMapping("/deletes")
	@ResponseBody
	public int dele(String id) {
		String[] split = id.split(",");
		for (String string : split) {
			payBillService.deletePayBill(Integer.parseInt(string));
		}
		return split.length;
	}
	
	@RequestMapping("/toUpdate")
	public String toUpdate(Integer id,Model model) {
		PayBill bill = payBillService.selectPayBillById(id);
		model.addAttribute("bill", bill);
		return "payBill_update";
	}
	
	@RequestMapping("/update")
	public String update(@Validated @ModelAttribute("bill")PayBill bill,BindingResult result,MultipartFile file,Model model) throws IllegalStateException, IOException {
		if(result.hasErrors()) {
			return "payBill_update";
		}
		if(!file.isEmpty()) {
			String originalFilename = file.getOriginalFilename();
			String fileName = System.currentTimeMillis()+"_"+originalFilename;
			File destFile = new File("d:\\imgpic\\"+fileName);
			//复制文件
			file.transferTo(destFile);
			bill.setBills("/imgpic/"+fileName);
		}
		payBillService.updatePayBill(bill);
		return "redirect:list";
	}
	
	
	public void a(){
		System.out.println("11");
	}
	
}
